﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;

public partial class AdminX_Testimonial_Testimonial : System.Web.UI.UserControl
{
    private int nDefaulLang = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            ShowMess();
            SetLable();
            BindDataToGrid(1);
        }
    }
    private void SetLable()
    {
        btnAddVideo.Text = MultipleLanguage.BTN_ADDNEW[nDefaulLang - 1];
    }

    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;

        DataTable dtb = new DataTable();
        Testimonial objTestimonial = new Testimonial();
        dtb = objTestimonial.Search(CurrentPageIndex, nPageSize, ref nPageCount);
        dtgTestimonial.DataSource = dtb;
        dtgTestimonial.DataBind();

        VisiblePaging(dtb.Rows.Count);
        if (dtb.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    protected void dtgTestimonial_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Testimonial obj = new Testimonial();
        obj.LoadById(nID);
        switch (strCommand)
        {
            case "Delete":
                int nDelete = obj.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
            case "Active":
                int nActive = obj.UpdateStatus(nID, EnumeType.INACTIVE);
                if (nActive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Inactive":
                int nInactive = obj.UpdateStatus(nID, EnumeType.ACTIVE);
                if (nInactive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

        }
    }
    protected void dtgTestimonial_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title"));

            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));

            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaulLang - 1];
            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaulLang - 1]);
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaulLang - 1];
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaulLang - 1]);

            lnkDelete.Text = MultipleLanguage.BTN_DELETE[nDefaulLang - 1];
            lnkDelete.Enabled = true;
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaulLang]);

            lnkEdit.Text = MultipleLanguage.BTN_EDIT[nDefaulLang - 1];
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TestimonialID"));
        }
    }

    protected void btnAddVideo_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TESTIMONIAL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }

    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nDefaulLang = Support.GetDefaultLanguage();
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaulLang];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaulLang]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaulLang]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaulLang]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion
    
}